
package com.peach.inquiry;

import android.app.Activity;
import android.content.Context;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;


public class InquiryPlugin implements FlutterPlugin, ActivityAware {

    public static final String CHANNEL = "plugins.peach.com/inquiry";
    private MethodCallHandler handler;
    private MethodChannel methodChannel;
    private IM im;

    // This static function is optional and equivalent to onAttachedToEngine. It supports the old
    // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
    // plugin registration via this function while apps migrate to use the new Android APIs
    // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
    //
    // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
    // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
    // depending on the user's project. onAttachedToEngine or registerWith must both be defined
    // in the same class.
    @SuppressWarnings("deprecation")
    public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) {
        InquiryPlugin plugin = new InquiryPlugin();
        plugin.setUpChannel(registrar.context(), registrar.activity(), registrar.messenger());
    }

    @Override
    public void onAttachedToEngine(FlutterPluginBinding binding) {
        setUpChannel(binding.getApplicationContext(), null, binding.getBinaryMessenger());
    }

    @Override
    public void onDetachedFromEngine(FlutterPluginBinding binding) {
        methodChannel.setMethodCallHandler(null);
        methodChannel = null;
    }

    @Override
    public void onAttachedToActivity(ActivityPluginBinding binding) {
        IM.staticActivity = binding.getActivity();
    }

    @Override
    public void onDetachedFromActivity() {
        tearDownChannel();
    }

    @Override
    public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
        onAttachedToActivity(binding);
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        onDetachedFromActivity();
    }

    private void setUpChannel(Context context, Activity activity, BinaryMessenger messenger) {
        methodChannel = new MethodChannel(messenger, CHANNEL);
        im = new IM(context, activity, methodChannel);
        handler = new MethodCallHandler(im);
        methodChannel.setMethodCallHandler(handler);
    }

    private void tearDownChannel() {
        im.setActivity(null);
        methodChannel.setMethodCallHandler(null);
    }
}
